package com.gfxy.common.portal.service;

import com.gfxy.common.portal.dao.*;
import com.gfxy.common.portal.entity.*;
import com.gfxy.commons.utils.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lijixin 2019-05-15
 */
@Service
public class RoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysAppTreeMapper appTreeMapper;

    @Autowired
    private SysAppButtonMapper appButtonMapper;

    public List<SysRole> getRoleInfo(){
        return roleMapper.selectByExample(null);
    }

    public SysRole save(SysRole role){
        if(role.getRoleId()==null || role.getRoleId().isEmpty()){
            role.setRoleId(UUIDUtil.getUUID());
            roleMapper.insert(role);
        } else {
            roleMapper.updateByPrimaryKey(role);
        }
        return role;
    }

    public void delete(String roleId){
        roleMapper.deleteByPrimaryKey(roleId);
    }


    public Map<String,Object> getMenuAuth(String menuId,String userName){
        Map<String,Object> result = new HashMap<String, Object>();
        if(userName.equals("admin")){
            SysAppTree appTree = appTreeMapper.getAdminAppById(menuId);
            if(appTree==null){
                result.put("isPageAuth", "0");
            } else {
                result.put("isPageAuth", "1");
                result.put("openUrl", appTree.getUrl());
                result.put("btns", "all");
            }
        } else {
            SysAppTree appTree = appTreeMapper.getUserAppById(userName, menuId);
            if (appTree == null) {
                result.put("isPageAuth", "0");
            } else {
                result.put("isPageAuth", "1");
                result.put("openUrl", appTree.getUrl());
                List<SysAppButton> tawAppBtns = getBtnByMenu(menuId);
                if (tawAppBtns.size() == 0) {
                    result.put("btns", "all");
                } else {
                    result.put("btns", "some");
                    List<SysAppButton> reRoleResourcesList = getRoleBtnList(userName, menuId);
                    result.put("btnList", reRoleResourcesList);
                }
            }
        }
        return result;
    }

    private List<SysAppButton> getBtnByMenu(String menuId) {
        SysAppButtonExample appButtonExample = new SysAppButtonExample();
        SysAppButtonExample.Criteria criteria = appButtonExample.createCriteria();
        criteria.andAppIdEqualTo(menuId);
        return appButtonMapper.selectByExample(appButtonExample);
    }


    private List<SysAppButton> getRoleBtnList(String userName, String menuId) {
        return appButtonMapper.getRoleBtnList(userName, menuId);
    }

    public List<SysRole> getRoleByRoleId(String roleId) {
        SysRoleExample example = new SysRoleExample();
        example.createCriteria().andRoleIdGreaterThanOrEqualTo(roleId);

        return roleMapper.selectByExample(example);
    }

    /**
     * 获取用户拥有的权限
     * @param userId
     * @return
     */
    public List<SysRole> getUserRole(String userId){

        return roleMapper.getUserRole(userId);
    }

    /**
     * 获取用户没有的权限
     * @param userId
     * @return
     */
    public List<SysRole> getUserNoRole(String userId){
        return roleMapper.getUserNoRole(userId);
    }


}
